package com.david.func_test.activity

import android.app.Activity
import android.content.Context
import android.graphics.PointF
import android.os.Bundle
import android.util.DisplayMetrics
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.LinearSmoothScroller
import androidx.recyclerview.widget.RecyclerView
import com.david.func_test.R


class RecyclerTestActivity:Activity() {
    private lateinit var recycler: RecyclerView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_recycler_test)
        recycler = findViewById<RecyclerView>(R.id.recycler)
//        recycler.layoutManager = ScrollSpeedLinearLayoutManger(this)
        recycler.layoutManager = LinearLayoutManager(this)
        recycler.adapter = TestAdapter()
        recycler.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                super.onScrollStateChanged(recyclerView, newState)
//                val firstVisiableChildView = this.getChildAt(0);
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
//                    val layoutManager = recycler.getLayoutManager() as LinearLayoutManager
//                    val firstPosition = layoutManager.findFirstVisibleItemPosition()
//                    val completePosition = layoutManager.findFirstCompletelyVisibleItemPosition()
//                    if (completePosition != firstPosition)
//                        recycler.smoothScrollToPosition(firstPosition)
                }
            }
        })
    }
}


class TestAdapter: RecyclerView.Adapter<TestHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TestHolder {
        val title = TextView(parent.context)
        title.setPadding(0, 80, 0, 80)
        return TestHolder(title)
    }

    override fun onBindViewHolder(holder: TestHolder, position: Int) {
        holder.title.text = "position is $position"
    }

    override fun getItemCount(): Int {
        return 50
    }

}

class TestHolder(var title: TextView): RecyclerView.ViewHolder(title){
}

/**
 * 控制滑动速度的LinearLayoutManager
 */
class ScrollSpeedLinearLayoutManger(context: Context) : LinearLayoutManager(context) {
    private var MILLISECONDS_PER_INCH = 0.03f
    private val contxt: Context
    override fun smoothScrollToPosition(
        recyclerView: RecyclerView,
        state: RecyclerView.State,
        position: Int
    ) {
        val linearSmoothScroller: LinearSmoothScroller =
            object : LinearSmoothScroller(recyclerView.context) {
                override fun computeScrollVectorForPosition(targetPosition: Int): PointF? {
                    return this@ScrollSpeedLinearLayoutManger
                        .computeScrollVectorForPosition(targetPosition)
                }

                //This returns the milliseconds it takes to
                //scroll one pixel.
                override fun calculateSpeedPerPixel(displayMetrics: DisplayMetrics): Float {
                    return MILLISECONDS_PER_INCH / displayMetrics.density
                    //返回滑动一个pixel需要多少毫秒
                }
            }
        linearSmoothScroller.targetPosition = position
        startSmoothScroll(linearSmoothScroller)
    }

    fun setSpeedSlow() {
        //自己在这里用density去乘，希望不同分辨率设备上滑动速度相同
        //0.3f是自己估摸的一个值，可以根据不同需求自己修改
        MILLISECONDS_PER_INCH = contxt.getResources().getDisplayMetrics().density * 0.3f
    }

    fun setSpeedFast() {
        MILLISECONDS_PER_INCH = contxt.getResources().getDisplayMetrics().density * 0.03f
    }

    init {
        contxt = context

        setSpeedSlow()
    }
}